package com.team12.hotel404.controller.admin;

import com.team12.hotel404.domain.User;
import com.team12.hotel404.service.AdminService;
import com.team12.hotel404.service.RolePowerService;
import com.team12.hotel404.service.UserRoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.naming.AuthenticationException;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RolePowerService rolePowerService;

    /**
     * 拉取登录信息
     * @param adminName 登录名
     * @param adminPwd  登录口令
     * @return 跳转到登录界面
     */
    @PostMapping(value = "/login")
//    @ResponseBody
    public String admin_login(Model model, @RequestParam("adminName")String adminName, @RequestParam("adminPwd")String adminPwd,HttpSession session){
//        System.out.println(adminName+" "+adminPwd);
        //ser user = adminService.findUserByPrimaryId(adminId);
        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(adminName,adminPwd);
            currentUser.login(token);
            return "admin/admin-index";
        }
        return "common/error";
//        User user = adminService.findByName(adminName);
//        if(user != null){
//            if(user.getUserPassword().equals(adminPwd)){
//                session.setAttribute("user",user);
//                return "/admin/admin-index";
//            }else{
//                model.addAttribute("msg","密码错误");
//                return "/admin/login";
//            }
//        }else{
//            model.addAttribute("msg","用户不存在");
//            return "/admin/login";
//        }

    }

    @RequestMapping(value = "/editcurradmininfo",method = RequestMethod.GET)
    public String edit_current_admin_info(){
        return "admin/current-admin-info";
    }
    /**
     * 管理员退出系统
     * @param session
     * @return 返回到管理员登录界面
     */
    @GetMapping(value = "logout")
    public String admin_logou(HttpSession session){
        session.invalidate();
        return "redirect:login";
    }

    @PostMapping(value = "update")
    public String admin_update(Model model){
        return null;
    }

    /**
     * 跳转到登陆界面
     * @return 管理员登录界面
     */
    @GetMapping(value = "/login")
    public String admin_login(){
        return "admin/login";
    }
}
